package controllers;

import helpers.CrawlerHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import services.Blogger;
import services.WordPress;

import models.AutoSite;
import models.Blog;
import models.News;
import models.User;
import data.AutoSiteData;
import data.BlogData;
import data.UserData;
import debugs.AutoSiteDebug;
import forms.AutoSiteForm;

public class GetContentServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		String gmail = request.getUserPrincipal().getName();
		
		//Get Blog Id
		String blogId = (String) request.getParameter("blogId");
		
		//Get list site in blog
		AutoSiteData aData = new AutoSiteData();
		ArrayList<AutoSite> sites = aData.getByBlogId(blogId);
		//Get blog info
		BlogData blogData = new BlogData();
		Blog blog = blogData.getById(blogId);
		if (blog == null) {
			response.sendRedirect("/message?txt=page-not-found");
			return;
		}
		
		AutoSite autoSite = new AutoSite();
		TreeMap check = new TreeMap();
		
		request.setAttribute("check", check);
		request.setAttribute("blog", blog);
		request.setAttribute("sites", sites);
		request.setAttribute("autoSite", autoSite);
		request.setAttribute("form", "add");
		try {
	        request.getRequestDispatcher("/pages/form_content.jsp").forward(request, response);
	    } catch (Exception e) {
	        //TODO: handle exception
	        e.printStackTrace();
	    }
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		String gmail = request.getUserPrincipal().getName();
		
		//Get blogId
		String blogId = (String) request.getParameter("blogId");
		String category = (String) request.getParameter("category");
		//Get list site in blog
		AutoSiteData aData = new AutoSiteData();
		
		//Get blog info
		BlogData blogData = new BlogData();
		Blog blog = blogData.getById(blogId);
		//Set site info
		AutoSite autoSite = new AutoSite();
		autoSite.setBlogId(blogId);
		//Get value form and check valid
		TreeMap form = AutoSiteForm.checkValidStaticContent(request, autoSite);
		autoSite =(AutoSite) form.get("autoSite");
		TreeMap check = (TreeMap) form.get("checkValid");
		
		//Save data
		if (check.size() == 0){
			News news = CrawlerHelper.getNews(autoSite.getSite(), autoSite);
			if (category != null) {
				List<String> labels = new ArrayList<String>();
				String[] cats = category.split(",");
				for (int i = 0; i < cats.length; i++) {
					if (!cats[i].isEmpty()) {
						labels.add(cats[i]);
					}
				}
				news.setLabels(labels);
			}
			
			if (blog.getType() != null && blog.getType().equalsIgnoreCase("wordpress")) {
				WordPress wp = new WordPress(blog);
				try {
					int id = wp.createPost(news);
					if (id != 0) {
						request.setAttribute("success", "Add content success with blogger " + blogId);
					} else {
						check.put("unsucess", "Can't post content to blogger " + blogId + ". Please check Blog Url, Username or Password again!");
					}
				} catch (Exception e) {
					check.put("unsucess", "Can't post content to blogger " + blogId + ". Please check Blog Url, Username or Password again!");
				}
			} else {	
				//Post news to blogger
				Blogger blogger = new Blogger(blog);
				try {
					blogger.createPost(news);
					request.setAttribute("success", "Add content success with blogger " + blogId);
				} catch (Exception e) {
					check.put("unsucess", "Can't post content to blogger " + blogId + ". Please check blogid, email or password again!");
					e.printStackTrace();
				}
			}
			autoSite = new AutoSite();
		}
		
		ArrayList<AutoSite> sites = aData.getByBlogId(blogId);
		
		request.setAttribute("check", check);
		request.setAttribute("blog", blog);
		request.setAttribute("sites", sites);
		request.setAttribute("autoSite", autoSite);
		request.setAttribute("form", "add");
		try {
	        request.getRequestDispatcher("/pages/form_content.jsp").forward(request, response);
	    } catch (Exception e) {
	        //TODO: handle exception
	        e.printStackTrace();
	    }
	}
}
